{{{id=1| # slightly modified from the interact webpage def picard_iteration(f, t0, u0, iterations): ''' Computes the N-th Picard iterate for the IVP u'(t) = f(t,u(t)), for all t, u(t0) = u0. ''' if iterations == 0: u1 = lambda t: u0 + 2*t-t^2 return expand(u1(t)) for i in range(iterations): u_old = lambda s: picard_iteration(f, t0, u0, iterations-1).subs(t=s) un = lambda t: u0 + integral(f(t=s,u=u_old(s)), s, t0, t) return expand(un(t)) @interact def picarder(n_iterations = slider(0,20,1,default = 2)): var('y,u,t,s') f = lambda t,u: u y = function('y',t) # define x to be a function of that variable DE = diff(y, t) - f(t,y) g=desolve(DE,[y,t],[0,1]) exact = plot(g,(t,0,2)) html('The exact solution is:
') html('$'+latex(g)+'$') for i in range(n_iterations): pic = picard_iteration(f,0,1,i) html('The Picard iteration approximation after ' + str(i) + ' iterations is:
') html('$'+latex(pic)+'$') exact = exact+plot(pic,(t,0,2), rgbcolor = (1,0,0)) show(exact) ///
n_iterations 
}}} {{{id=5| # slightly modified from the interact webpage x,y = var('x,y') from sage.ext.fast_eval import fast_float @interact def _(f = input_box(default=1), g=input_box(default=y^2), xmin=input_box(default=-3), xmax=input_box(default=3), ymin=input_box(default=-3), ymax=input_box(default=3), start_x=input_box(default=0.5), start_y=input_box(default=0.5), step_size=(0.01,(0.001, 0.2)), steps=(600,(0, 1400)) ): ff = fast_float(f, 'x', 'y') gg = fast_float(g, 'x', 'y') steps = int(steps) points = [ (start_x, start_y) ] for i in range(steps): xx, yy = points[-1] try: points.append( (xx + step_size * ff(xx,yy), yy + step_size * gg(xx,yy)) ) except (ValueError, ArithmeticError, TypeError): break starting_point = point(points[0], pointsize=50) solution = line(points) vector_field = plot_vector_field( (f,g), (x,xmin,xmax), (y,ymin,ymax) ) result = vector_field + starting_point + solution html(r"$\displaystyle\frac{dx}{dt} = %s$ $ \displaystyle\frac{dy}{dt} = %s$" % (latex(f),latex(g))) result.show(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax) ///
xmin 
xmax 
ymin 
ymax 
start_x 
start_y 
step_size 
steps 
}}} {{{id=6| /// }}}